package com.wjj.sixinone.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wjj.sixinone.common.enums.ExceptionEnum;
import com.wjj.sixinone.common.exception.GlobalException;
import com.wjj.sixinone.common.pojo.dto.DrugDTO;
import com.wjj.sixinone.common.pojo.entity.Drug;
import com.wjj.sixinone.common.pojo.entity.User;
import com.wjj.sixinone.common.pojo.excel.DrugExcel;
import com.wjj.sixinone.common.pojo.excel.UserExcel;
import com.wjj.sixinone.common.pojo.vo.DrugFormVO;
import com.wjj.sixinone.common.pojo.vo.DrugVO;
import com.wjj.sixinone.common.util.Result;
import com.wjj.sixinone.handler.ExcelExportHandle;
import com.wjj.sixinone.mapper.DrugMapper;
import com.wjj.sixinone.service.DrugService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjj.sixinone.service.minio.MinioFileService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 小王不秃头
 * @since 2024-07-22
 */
@Service
public class DrugServiceImpl extends ServiceImpl<DrugMapper, Drug> implements DrugService {

    @Autowired
    private ExcelExportHandle excelExportHandle;
    @Autowired
    private DrugMapper drugMapper;
    @Autowired
    private MinioFileService minioFileService;
    @Override
    public Result selectDrugByLimit(DrugVO drugVO) {
        Integer page = drugVO.getPage();
        Integer limit = drugVO.getLimit();
        int index = (page - 1) * limit;
        DrugDTO drugDTO = new DrugDTO();
        BeanUtils.copyProperties(drugVO,drugDTO);
        drugDTO.setIndex(index);
       List<Drug> list= drugMapper.selectDrugByLimit(drugDTO);

        Integer count=drugMapper.count(drugDTO);

        return Result.ok().put("data",list).put("count",count);
    }

    @Override
    public void uploadExcel(HttpServletResponse response) {
        List<Drug> list = drugMapper.selectList(new LambdaQueryWrapper<Drug>().select(Drug::getId,
                Drug::getDrugName, Drug::getDrugNo, Drug::getDrugPrice,
                Drug::getDrugQuantity, Drug::getDrugRen, Drug::getImg,
                Drug::getUpdateUser, Drug::getCreateUser, Drug::getCreateTime, Drug::getUpdateTime));

        List<DrugExcel> userExcels = new ArrayList<>();
        for (Drug drug : list) {

            DrugExcel userExcel = new DrugExcel();
            BeanUtils.copyProperties(drug, userExcel);
            userExcels.add(userExcel);
        }
        excelExportHandle.export(response, "药品信息", userExcels, DrugExcel.class);

    }

    @Override
    public Result removeDrugById(String id,String img) {
        if(img!=null) {
            minioFileService.deleteFileFromMinio(img);
        }
        int i = drugMapper.deleteById(id);
        if(i<1){
            throw new GlobalException(ExceptionEnum.DELETE_ERROR);
        }
        return Result.ok();

    }

    @Override
    public Result updateDrugById(DrugFormVO drugFormVO) {
        Drug drug = new Drug();

        BeanUtils.copyProperties(drugFormVO,drug);
        if(drugMapper.updateById(drug)<1){
            throw new GlobalException(ExceptionEnum.UPDATE_ERROR);
        }
        return Result.ok();
    }

    @Override
    public Result addDrug(DrugFormVO drugFormVO) {
        String img = drugFormVO.getImg();
        String url=null;
        if(img!=null){
             url = minioFileService.uploadBase64File(img);
        }
        if(url == null){
           throw  new GlobalException(ExceptionEnum.ERROR_MINIO_SEND);
        }
        Drug drug = new Drug();
        BeanUtils.copyProperties(drugFormVO,drug);
        drug.setImg(url);
        if(drugMapper.insert(drug)<1){
            throw new GlobalException(ExceptionEnum.INSERT_ERROR);
        }
        return Result.ok();
    }
}
